﻿// ע�

using EcsDebugSimulator.Models;
using EcsDebugSimulator.Services.SimulatorHandle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EcsDebugSimulator.Services.SimulatorHandle;

/// <summary>
/// 模拟器Socket处理器
/// </summary>
public class SimulatorHandleSocket : SimulatorHandleBase
{

   

    public override async Task<bool> HandleMsg(SimulatorGeneralDto msg,CommandContent commandContent,string equipType)
    {
        switch (msg.Command)
        {
            case "S101":
                S101Command(msg, commandContent, equipType);
                break;;
            case "S102":
                S102Command(msg, commandContent, equipType);
                break;
            case "S103":
                S103Command(msg, commandContent, equipType);
                break;
            case "S104":
                S104Command(msg, commandContent, equipType);
                break;
            case "S105":
                S105Command(msg, commandContent, equipType);
                break;
            case "S106":
                S106Command(msg, commandContent, equipType);
                break;
            case "S107":
                S107Command(msg, commandContent, equipType);
                break;

                break;
        }
        return true;
    }

    /// <summary>
    /// 托盘到达
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>

    #region 通用命令
    /// <summary>
    /// 上报设备状态
    /// </summary>
    /// <returns></returns>
    private async Task S101Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {

            dto.Content = StringHand(commandContent.MachineScreen.EquipStatus,7);
        }
        else if (equipType == "机械手-组盘")
        {
            dto.Content = StringHand(commandContent.MachineGruop.EquipStatus,7);
        }
        else if (equipType == "机械手-拆盘")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.EquipStatus,7);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.Ocv.EquipStatus,7);
        }
        else
        {
            return;
        }
        dto.Command = "E101";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);
    }

    /// <summary>
    /// 上报设备异常
    /// </summary>
    /// <returns></returns>
    private async Task S110Command(SimulatorGeneralDto generalDto)
    {

    }


    /// <summary>
    /// ECS通知设备数据异常
    /// </summary>
    /// <returns></returns>
    private async Task E110Command(SimulatorGeneralDto generalDto)
    {

    }

    /// <summary>
    /// ECS向设备请求异常复位
    /// </summary>
    /// <returns></returns>
    private async Task E100Command(SimulatorGeneralDto generalDto)
    {

    }

    /// <summary>
    /// ECS向设备请求停止或暂停当前作业
    /// </summary>
    /// <returns></returns>
    private async Task E204Command(SimulatorGeneralDto generalDto)
    {

    }

    /// <summary>
    /// ECS向设备请求再开始作业
    /// </summary>
    /// <returns></returns>
    private async Task E200Command(SimulatorGeneralDto generalDto)
    {

    }
    #endregion

    #region 机械手-筛选

    /// <summary>
    /// 托盘到达
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>
    private async Task S102Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {
            dto.Content = StringHand(commandContent.MachineScreen.TrayArrive,5);
        }
        else if (equipType == "机械手-组盘")
        {
            dto.Content = StringHand(commandContent.MachineGruop.TrayArrive, 5);
        }
        else if (equipType == "机械手-拆盘")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.TrayArrive,5);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.Ocv.TrayArrive,5);
        }
        else
        {
            return;
        }
        dto.Command = "E102";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);
    }

    /// <summary>
    /// 请求位置信息
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>
    private async Task S103Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {
            dto.Content = StringHand(commandContent.MachineScreen.NgInfo, 4);
        }
        else if (equipType == "机械手-拆盘")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.CellLocation, 4);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.CellLocation, 5);
        }

        dto.Command = "E103";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);

    }

    /// <summary>
    /// 作业开始
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>
    private async Task S104Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {
            dto.Content = StringHand(commandContent.MachineScreen.EquipStartJob, 5);
        }
        else if (equipType == "机械手-组盘")
        {
            dto.Content = StringHand(commandContent.MachineGruop.EquipStartJob, 5);
        }
        else if (equipType == "机械手-拆盘")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.EquipStartJob, 5);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.Ocv.EquipStartJob, 5);
        }
        else
        {
            return;
        }
        dto.Command = "E104";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);
    }

    /// <summary>
    /// 结果上报
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>
    private async Task S105Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {
            dto.Content = StringHand(commandContent.MachineScreen.SelectResult, 5);
        }
        else if (equipType == "机械手-组盘")
        {
            dto.Content = StringHand(commandContent.MachineGruop.CaptureCellResult, 5);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.Ocv.OcvRecipe, 5);
        }
        else
        {
            return;
        }
        dto.Command = "E105";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);
    }

    /// <summary>
    /// 作业结束
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>
    private async Task S106Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {
            dto.Content = StringHand(commandContent.MachineScreen.EquipEndJob, 5);
        }
        else if (equipType == "机械手-组盘")
        {
            dto.Content = StringHand(commandContent.MachineGruop.EquipEndJob, 5);
        }
        else if (equipType == "机械手-拆盘")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.EquipEndJob, 5);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.Ocv.EquipEndJob, 5);
        }
        else
        {
            return;
        }
        dto.Command = "E106";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);
    }

    /// <summary>
    /// 作业结束
    /// </summary>
    /// <param name="generalDto"></param>
    /// <param name="commandContent"></param>
    /// <param name="equipType"></param>
    /// <returns></returns>
    private async Task S107Command(SimulatorGeneralDto generalDto, CommandContent commandContent, string equipType)
    {
        //回复dto
        SimulatorGeneralDto dto = new SimulatorGeneralDto();

        if (equipType == "机械手-筛选")
        {
            dto.Content = StringHand(commandContent.MachineScreen.TrayLeave, 5);
        }
        else if (equipType == "机械手-组盘")
        {
            dto.Content = StringHand(commandContent.MachineGruop.TrayLeave, 5);
        }
        else if (equipType == "机械手-拆盘")
        {
            dto.Content = StringHand(commandContent.MachineExcrete.TrayLeave, 5);
        }
        else if (equipType == "Ocv")
        {
            dto.Content = StringHand(commandContent.Ocv.TrayLeave, 5);
        }
        else
        {
            return;
        }
        dto.Command = "E107";
        dto.EquipCode = StringHand(generalDto.EquipCode, 8);
        dto.SendTime = DateTime.Now.ToString("yyyymmddhhMMss");
        InvokeSend(dto);
    }
    #endregion

    #region 机械手-组盘

    #endregion

    #region 机械手-拆盘

    #endregion

    #region Ocv

    #endregion

    public string StringHand(string content,int len)
    {
        string str=string.Empty;
        if(content == null)
        {
            return str.PadRight(len);
        }
        else
        {
            str = content.Trim();
            return content.PadRight(len);
        }
    }
}
